package com.itmk.web.sys_menu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.utils.Result;
import com.itmk.web.sys_menu.entity.AssignTreeParm;
import com.itmk.web.sys_menu.entity.MakeMenuTree;
import com.itmk.web.sys_menu.entity.RouterVo;
import com.itmk.web.sys_menu.entity.SysMenu;
import com.itmk.web.sys_menu.service.SysMenuService;
import com.itmk.web.sys_role.entity.RoleParm;
import com.itmk.web.sys_role.entity.SysRole;
import com.itmk.web.sys_user.entity.SysUser;
import com.itmk.web.sys_user.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.expression.spel.ast.Assign;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.crypto.spec.OAEPParameterSpec;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/menu")
public class SysMenuController {
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private SysUserService sysUserService;

    //新增
    @PostMapping
    public Result<Object> add(@RequestBody SysMenu sysMenu) {
        sysMenu.setCreateTime(new Date());
        if (sysMenuService.save(sysMenu)) {
            return Result.ok().message("新增菜单成功");
        }
        return Result.fail().message("新增菜单失败");
    }
    //编辑
    @PutMapping
    public Result<Object> update(@RequestBody SysMenu SysMenu) {
        SysMenu.setUpdateTime(new Date());
        if (sysMenuService.updateById(SysMenu)) {
            return Result.ok().message("编辑菜单成功");
        }
        return Result.fail().message("编辑菜单失败");
    }
    //删除
    @DeleteMapping("/{menuId}")
    public Result<Object> delete(@PathVariable(value="menuId") Long menuId) {
        System.out.println("menuId:" + menuId);
        //如果存在下级菜单就不能删除
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getParentId, menuId);
        List<SysMenu> list = sysMenuService.list(queryWrapper);
        if(!list.isEmpty()){
            return Result.fail().message("有下级菜单，删除失败");
        }

        if (sysMenuService.removeById(menuId)) {
            return Result.ok().message("删除菜单成功");
        }
        return Result.fail().message("删除菜单失败");
    }
    //查询列表
    @GetMapping("/getList")
    public Result getList() {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SysMenu::getOrderNum);
        //查询出所有的菜单
        List<SysMenu> list = sysMenuService.list(queryWrapper);
        //组装树形菜单
        List<SysMenu> menuList = MakeMenuTree.makeTree(list, 0L);
        return Result.ok(menuList).message("查询菜单列表成功");
    }
    //查询上级菜单
    @GetMapping("/getParent")
    public Result getParent() {
        List<SysMenu> menuList = sysMenuService.getParent();
        return Result.ok(menuList).message("查询上级菜单列表成功");
    }
    //获取前端动态路由菜单
    @GetMapping("/getMenuList")
    public Result getMenuList(Long userId){
        SysUser user = sysUserService.getById(userId);
        List<SysMenu> menuList = null;
        if(StringUtils.isNotEmpty(user.getIsAdmin()) && "1".equals(user.getIsAdmin())){
            LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByAsc(SysMenu::getOrderNum);
            menuList = sysMenuService.list(queryWrapper);
        }else{
            menuList = sysMenuService.getMenuByUserId(userId);
        }
        //去掉按钮数据
        List<SysMenu> collect = Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item ->item!=null && StringUtils.isNotEmpty(item.getType()) && !item.getType().equals("2"))
                .collect(Collectors.toList());
        List<RouterVo> router = MakeMenuTree.makeRouter(collect, 0L);
        System.out.println(router);
        return Result.ok(router).message("查询动态菜单数据成功");
    }




}
